package cn.onlineclass.controller;

import cn.onlineclass.entity.OResource;
import cn.onlineclass.service.resourceService;
import cn.onlineclass.util.AjaxPage;
import cn.onlineclass.util.AjaxResult;
import cn.onlineclass.util.StringUtil;
import com.alibaba.fastjson.JSON;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class ReourceController {
    public AjaxPage search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo = StringUtil.toInt(request.getParameter("pageNo"), 1);
        String majorid = request.getParameter("majorid");
        boolean inuse= Boolean.parseBoolean(request.getParameter("inuse"));
        AjaxPage p = resourceService.TYPE.search(pageNo, 10,inuse,majorid);
//        String str = JSON.toJSONStringWithDateFormat(p, "yyyy-MM-dd", SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullListAsEmpty, SerializerFeature.WriteNonStringValueAsString);
//        response.getWriter().print(str);
        return p;
    }

    public AjaxResult add(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        String tiele = request.getParameter("title");
        String url = request.getParameter("url");
        String icon = request.getParameter("icon");
        boolean inuse = Boolean.parseBoolean(request.getParameter("inuse"));
        long majorid = Long.parseLong(request.getParameter("majorid"));
        OResource c = new OResource();
        c.setTitle(tiele);
        c.setUrl(url);
        c.setIcon(icon);
        c.setInuse(inuse);
        c.setMajorid(majorid);
        resourceService con = new resourceService();
        int num = con.add(c);
        if (num>0){
            return AjaxResult.success("保存成功");
        }
//        String s = JSON.toJSONString(num);
//        response.getWriter().print(s);
        return AjaxResult.error("保存失败");
    }
    public void toupdate(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
            long id = StringUtil.toInt(request.getParameter("id"));
            resourceService s = new resourceService();
            OResource toupdate = s.get(id);
            String str = JSON.toJSONString(toupdate);
            response.getWriter().print(str);
    }
    public AjaxResult update(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int id = StringUtil.toInt(request.getParameter("id"));
        String tiele = request.getParameter("tiele");
        String url = request.getParameter("url");
        String icon = request.getParameter("icon");
        boolean inuse = Boolean.parseBoolean(request.getParameter("inuse"));
        long majorid = Long.parseLong(request.getParameter("majorid"));
        OResource c = new OResource();
        c.setTitle(tiele);
        c.setUrl(url);
        c.setIcon(icon);
        c.setInuse(inuse);
        c.setMajorid(majorid);
        c.setId(id);
        resourceService con = new resourceService();
        int num = con.update(c);
        if (num>0){
            return AjaxResult.success("修改成功");
        }
        return AjaxResult.error("修改失败");
    }
    public AjaxResult del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        OResource c=new OResource();
        int id = StringUtil.toInt(request.getParameter("id"));
        resourceService con = new resourceService();
        c.setId(id);
        c.setInuse(false);
        int num = con.del(c);
        if (num>0){
            return AjaxResult.success("删除成功");
        }
        return AjaxResult.error("删除失败");
    }
}

